﻿using System;
using Combat;
using UnityEngine;

namespace Visual
{
    public class PlayerDangerIndicator : MonoBehaviour
    {
        [SerializeField] private Health ring;
        [SerializeField] private CircleCollider2D ringCollider;

        private SpriteRenderer _spriteRenderer;
        
        private float _ringRange;
        private float _playerRadius;


        private void Awake()
        {
            _spriteRenderer = GetComponent<SpriteRenderer>();
            
            _ringRange = ringCollider.radius * ring.transform.localScale.x;
            _playerRadius = transform.parent.localScale.x / 2;
        }

        private void Update()
        {
            float distance = transform.position.magnitude + _playerRadius;
            if (ring.IsDead || distance > _ringRange)
            {
                _spriteRenderer.enabled = true;
            }
            else
            {
                _spriteRenderer.enabled = false;
            }
        }
    }
}